package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.role.SearchRoleBo;
import com.zhuss.bean.unit.core.role.RoleBean;
import com.zhuss.bean.unit.core.role.RoleVo;
import com.zhuss.bean.vo.core.role.ListRoleVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.core.db.dao.ext.RoleDao;
import com.zhuss.core.db.entity.Role;
import com.zhuss.exception.AabException;
import com.zhuss.exception.ExceptionCode;


/**
 * 查询角色 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("roleRead")
public class RoleRead {
	
	@Autowired
	private RoleDao roleDao;
	
	
	public Map<String, Role> listMap() {
		List<Role> list = roleDao.listAll(null);
		Map<String, Role> map = new HashMap<>();
		for (Role r : list) {
			map.put(r.getName(), r);
		}
		return map;
	}
	
	
	/**
	 * 根据ID 列表 查询角色列表
	 * @param 
	 * @return
	 */
	public List<RoleBean> listById(List<Long> roleIdList) {
		List<RoleBean> result = roleDao.listById(roleIdList);
		return result;
	}
	
	
	/**
	 * type
	 * 根据类型查询角色
	 * @param 
	 * @return
	 */
	public List<Long> listByType(Integer type) {
		List<Role> list = roleDao.listByType(type);
		List<Long> result = new ArrayList<Long>();
		for (Role role : list) {
			result.add(role.getRoleId());
		}
		return result;
	}
	
	
	/**
	 * 根据名字查询角色
	 * @param 
	 * @return
	 */
	public List<Role> list(String name) {
		List<Role> result = roleDao.listByName(name);
		return result;
	}
	
	public List<Role> listNotId(String name, Long roleId) {
		List<Role> result = new ArrayList<Role>();
		List<Role> list = list(name);
		for (Role c : list) {
			if (!c.getRoleId().equals(roleId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	
	/**
	 * 角色ID列表
	 * @return
	 */
	public Object listRoleId() {
		List<Role> roleList = roleDao.listAll(null);
		List<Long> result = new ArrayList<>();
		for (Role role : roleList) {
			result.add(role.getRoleId());
		}
		return result;
	}
	
	/**
	 * 角色列表
	 * @param roleIdList
	 * @return
	 */
	public Object getByList(List<Long> roleIdList) {
		List<Role> roleList = roleDao.getByList(roleIdList);
		List<RoleVo> result = new ArrayList<>();
		transferList2(roleList, result);
		return result;
	}
	
	private void transferList2(List<Role> roleList, List<RoleVo> result) {
		for (Role item : roleList) {
			RoleVo vo = new RoleVo();
			vo.setRoleId(item.getRoleId());
			vo.setRoleName(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * 角色
	 * @return
	 */
	public Object listRole() {
		List<Role> roleList = roleDao.listAll(null);
		List<ListVo> result = new ArrayList<>();
		transferList(roleList, result);
		return result;
	}
	
	/**
	 * 角色 ID
	 * @param status
	 * @return
	 */
	public Object listRoleId(Integer status) {
		List<Long> result = roleDao.listRoleId(status);
		return result;
	}
	
	private void transferList(List<Role> roleList, List<ListVo> result) {
		for (Role item : roleList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getRoleId());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * 角色列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchRoleBo bo) {
		PageVo<ListRoleVo> result = new PageVo<ListRoleVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(roleDao.list(bo));
        result.setTotal(roleDao.countList(bo));
		return result;
	}
	
	/**
	 * 角色详情
	 * @param roleId
	 * @return
	 * @throws AabException
	 */
	public Object detail(Long roleId) throws AabException {
		Object result = roleDao.detail(roleId);
		if (result == null) {
			throw new AabException(ExceptionCode.数据不存在);
		}
		return result;
	}
	
	

}
